package
{
	import flash.display.*;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class PinTool extends CursorTool
	{
		// private objects
		private var drawPoints:Array = null;
		private var overDrawPoint:DrawPoint = null;
		private var selectedDrawPoint:DrawPoint = null;
		// private vars
		private var _state:String;
		
		
		public function PinTool(drawPoints:Array)
		{
			super(this);
			this.drawPoints = drawPoints;
			this.mouseEnabled = false;
			this.mouseChildren = false;
			this.addEventListener(Event.ADDED, handleAdded);
		}
		private function handleAdded(event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			_state = "pin";
			drawCursor();
			// go through each point and add event listener
			for each (var p:DrawPoint in drawPoints)
			{
				p.mouseEnabled = true;
				if(!p.hasEventListener(MouseEvent.MOUSE_OVER)) p.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverPoint);
			}
			
			this.addEventListener(Event.REMOVED, handleRemoved);			
		}
		
		/** Mouse over should work for all points to highlight except currently selected, unless allow disconnected */
		private function handleMouseOverPoint(event:MouseEvent):void
		{
			overDrawPoint = DrawPoint(event.currentTarget);
			overDrawPoint.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverPoint);
			if (!overDrawPoint.ppinned)
			{
				overDrawPoint.addEventListener(MouseEvent.CLICK, handleClickPoint);	
				_state = "pin-yes";
			} else 
			{
				_state = "pin-no";	
			}
			drawCursor();
			overDrawPoint.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOutPoint);
		}
		private function handleMouseOutPoint(event:MouseEvent):void
		{
			overDrawPoint.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOutPoint);
			overDrawPoint.removeEventListener(MouseEvent.CLICK, handleClickPoint);
			_state = "pin";
			drawCursor();
			overDrawPoint.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverPoint);
			overDrawPoint = null;
		}
		/** The click handler either adds a new segment if another point is selected or selects this point if no point is selected */
		private function handleClickPoint(event:MouseEvent):void
		{
			//overDrawPoint.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOutPoint);
			overDrawPoint.removeEventListener(MouseEvent.CLICK, handleClickPoint);
			
			// (double-check) make sure this point is not pinned
			if (!overDrawPoint.ppinned)
			{ 
				if (overDrawPoint.pinned) { overDrawPoint.pin = false; }
				else { overDrawPoint.pin = true;  }		
				overDrawPoint.redrawSegments();
				overDrawPoint.redrawAngles();
				_state = "pin";
				drawCursor();
				//this.dispatchEvent(new Event(Event.COMPLETE));
				GeometryGameItem(parent.parent).processCommand("pin", "in_progress", this);
			}	
		}
		/** Process moves cursor, looks for out of bounds */
		override public function process(point:Point):void
		{			
			this.x = point.x;
			this.y = point.y;
			
		}
		
		private function drawCursor():void
		{
			// for pin
			var twidth:Number = 3;
			var theight:Number = 15;
			var hwidth:Number = 8;
			var hheight:Number = 8;
			
			switch (_state)
			{
				case "pin":
					//draw a pin, literally
					// tip						
					this.graphics.clear();
					this.graphics.lineStyle(twidth/8,0x666666);
					this.graphics.beginFill(0xCCCCCC);
					this.graphics.moveTo(0,0);
					this.graphics.lineTo(twidth/2,-twidth);
					this.graphics.lineTo(twidth/2,-theight);
					this.graphics.lineTo(-twidth/2,-theight);
					this.graphics.lineTo(-twidth/2,-twidth)
					this.graphics.lineTo(0,0);
					this.graphics.endFill();
					// handle
					this.graphics.beginFill(0xFFFFFF);
					this.graphics.drawEllipse(-hwidth/2,-theight,hwidth,hwidth/2);
					this.graphics.endFill();
					break;
				case "pin-yes":
					//draw a pin, literally
					// handle
					this.graphics.clear();
					this.graphics.lineStyle(twidth/8,0x666666);
					this.graphics.beginFill(0xFFFFFF);
					this.graphics.drawEllipse(-hwidth/2,-hwidth/2,hwidth,hwidth);
					this.graphics.endFill();
					break;
				case "pin-no":
					//draw a pin, literally
					// handle
					this.graphics.clear();
					this.graphics.lineStyle(twidth/8,0x666666);
					this.graphics.beginFill(0xFFFFFF);
					this.graphics.drawEllipse(-hwidth/2,-hwidth/2,hwidth,hwidth);
					this.graphics.endFill();
					this.graphics.lineStyle(1.5,0xFF0000);
					this.graphics.moveTo(-hwidth*Math.sqrt(2)/2,-hwidth*Math.sqrt(2)/2);
					this.graphics.lineTo(hwidth*Math.sqrt(2)/2,hwidth*Math.sqrt(2)/2);
					this.graphics.moveTo(hwidth*Math.sqrt(2)/2,-hwidth*Math.sqrt(2)/2);
					this.graphics.lineTo(-hwidth*Math.sqrt(2)/2,hwidth*Math.sqrt(2)/2);						
					break;
			}
		}
		
		/** On removal unproject and unoutline */
		private function handleRemoved(event:Event):void
		{
			this.removeEventListener(Event.REMOVED, handleRemoved);
			if (overDrawPoint != null)
			{
				overDrawPoint.enhance = false;
				overDrawPoint.highlight = false;
			}
			if (selectedDrawPoint != null)
			{
				selectedDrawPoint.enhance = false;
				selectedDrawPoint.highlight = false;
			}			
			for each (var p:DrawPoint in drawPoints)
			{
				if(p.hasEventListener(MouseEvent.MOUSE_OVER)) p.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverPoint);
				if(p.hasEventListener(MouseEvent.MOUSE_OUT)) p.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOutPoint);
				if(p.hasEventListener(MouseEvent.CLICK)) p.removeEventListener(MouseEvent.CLICK, handleClickPoint);
				p.mouseEnabled = false;
			}
		}
	}
}